package cc.lj.internal.merchant.application.service;

import cc.lj.internal.api.merchant.dto.PropertyResourcesApiDTO;
import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.merchant.domain.contract.model.vo.PropertyRelationDO;
import cc.lj.internal.merchant.domain.contract.model.vo.PropertyResourcesDO;
import cc.lj.internal.merchant.domain.contract.service.IPropertyRelationDomainService;
import cc.lj.internal.merchant.domain.contract.service.IPropertyResourcesDomainService;
import cc.lj.internal.merchant.interfaces.dto.convert.PropertyResourcesDTOConvertDO;
import lj.internal.dict.enums.EnumMerchantContractObject;
import lj.internal.dict.enums.EnumPropertyResourcesUseState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 物业资源展位表 app service
 * @author liangxifeng
 * @date 2021-12-25
 */
@Slf4j
@Service(value = "propertyResourcesAppService" )
public class PropertyResourcesAppService {
    //物业资源分类领域层service
    @Resource(name = "propertyRelationDomainServiceImpl")
    private IPropertyRelationDomainService relationDomainService;
    @Resource(name = "propertyResourcesDomainServiceImpl")
    private IPropertyResourcesDomainService resourcesDomainService;
    /**
     * 某个区域下的在租展位以及该展位的日租金接口List
     * @param relationId 物业资源分表property_relation主键
     * @return 在租展位以及该展位的日租金List
     * @param relationId
     * @return
     */

    public List<PropertyResourcesApiDTO> getRentingResourceListByArea(Integer relationId) {
        //通过relation_fid查询该区域下的所有展位list
        PropertyRelationDO relationDO = new PropertyRelationDO();
        relationDO.setRelationFid(relationId);
        List<PropertyRelationDO> relationList = relationDomainService.getList(relationDO);

        List<PropertyResourcesApiDTO> resourcesApiDTOList = new ArrayList<>();
        //循环查询到的List
        for (PropertyRelationDO propertyRelationDO : relationList) {
            //查询在租状态的展位(包含有日租金)
            try{
                PropertyResourcesDO resourcesDO = resourcesDomainService.getRentingResourceById(propertyRelationDO.getRelationId());
                //这里useState=8属于展位在租状态，但是实际上数据会存在该状态为在租，可是查询不到该展位在合同签订表数据，也就是最终计算的rentPrice=null
                //所以再次需要两个判断条件
                if(resourcesDO.getResourcesUseState()== EnumPropertyResourcesUseState.RENTING.getCode() && resourcesDO.getDayRentPrice() != null) //在租
                {
                    //DO to DTO
                    PropertyResourcesApiDTO resourcesApiDTO = PropertyResourcesDTOConvertDO.INSTANCE.convert(resourcesDO);
                    resourcesApiDTOList.add(resourcesApiDTO);
                }
            }catch (ServiceException ex) {
                //通过主键查询展位表为null,无数据
                if(ex.getCode() == 1002001001) {
                    log.info("error code="+ex.getCode()+","+ex.getMessage());
                    continue;
                }
            }
        }
        return resourcesApiDTOList;
    }
}
